<?php

namespace App\Admin\Controllers;

use App\Models\Goods;
use App\Models\Storage;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class StorageController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '本店存取酒';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Storage());

        $grid->number('序号');
        $grid->rows(function ($row, $number) {
            $row->column('number', $number+1);
        });
        $grid->column('name', __('Name'));
        $grid->column('mobile', __('Mobile'))->editable();
        $grid->column('goods.name', '商品');
        $grid->column('stock', __('Stock'))->editable();
        $grid->column('sell', __('Sell'));
        $grid->column('description', __('Description'));
        $grid->column('created_at', __('Created at'))->sortable();
        $grid->column('updated_at', __('Updated at'))->sortable();

        $grid->filter(function ($filter) {
            $filter->disableIdFilter();

            $filter->like('name', __('Name'));
            $filter->equal('mobile', __('Mobile'));
            $filter->equal('goods_id', '商品')->select(Goods::where('storaged', 1)->get()->pluck('name', 'id'));
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Storage::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('store_id', __('Store id'));
        $show->field('name', __('Name'));
        $show->field('mobile', __('Mobile'));
        $show->field('goods_id', __('Goods id'));
        $show->field('stock', __('Stock'));
        $show->field('sell', __('Sell'));
        $show->field('description', __('Description'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('deleted_at', __('Deleted at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Storage());

        $form->hidden('store_id', __('Store id'));
        $form->text('name', __('Name'))->rules('required');
        $form->mobile('mobile', __('Mobile'))
            ->rules('phone|required');
        $form->select('goods_id', '商品')
            ->options(Goods::where('storaged', 1)->get()->pluck('name', 'id'))
            ->required();
        $form->number('stock', __('Stock'))->rules('required');
        $form->number('sell', __('Sell'))->rules('required');
        $form->text('description', __('Description'));

        $form->saving(function (Form $form) {

            $form->store_id = Admin::user()->store_id;
        });
        return $form;
    }

    public function storeDetail()
    {
        
    }
}
